object c4 {
	def main(): Unit = {
		println(new case4().bubble());
	}
}

class case4 {
	var x: Int[];
	var tmp : Int;
	var swp: Bool;
	var incr: Int;
	var size: Int;
	var tab: String;

    def bubble(): Int = 
	{
		swp = true;
		x = new Int[5];
		x[0] = 5;
		x[1] = 2;
		x[2] = 7;
		x[3] = 4;
		x[4] = 9;
		incr = 0;
		size = 5;
		
		println("Avant le tri:");
		println(this.print());
		
		size = 5;
		if(this.bubble_sort())
		{
			size = 5;
		
			println("Apres le tri:");
		
			println(this.print());
		}
		return 0;		
        }

	def swap(a: Int, b: Int): Bool =
	{
		tmp = x[b];
		x[b] = x[a];
		x[a] = tmp;
		return true;
	}

	def bubble_sort(): Bool =
	{
		while(swp == true)
		{
			swp = false;
			incr = 0;
			while(!(incr == (size-1)))
			{
				if(!((x[incr] == x[incr+1]) || (x[incr] < x[incr+1])))
				{
					swp = this.swap(incr,incr+1);
				}
				incr = incr+1;
			}
			size = size-1;
		}
		return true; 	
	}
	
	def print(): String = 
	{
		tab = "[";
		while(!(5-size == 4))
		{
			tab= tab+x[5-size]+"|";
			size = size-1;
		}
		tab = tab+x[5-size];
		tab = tab+"]";
		return tab;
	}	
}
